function isObject(obj) {
  return (
    obj &&
    Object.prototype.toString.call(obj) === "[object Object]" &&
    Object.keys(obj).length
  );
}
const getPreviousNode = (prevId, processData) => {
  if (processData.nodeId === prevId) return processData;
  if (processData.childNode) {
    let r1 = getPreviousNode(prevId, processData.childNode);
    if (r1) {
      return r1;
    }
  }
  if (processData.conditionNodes) {
    for (let c of processData.conditionNodes) {
      let r2 = getPreviousNode(prevId, c);
      if (r2) {
        return r2;
      }
    }
  }
};

const handelGetEveyNode = (data, keyWithGoing) => {
  //递归拿到每个层级
  if (!isObject(data)) return;
  for (k in keyWithGoing) {
    let { outgoing, outingDeep } = keyWithGoing[k] || {};
    if (data.nodeId === k) {
      if (outgoing) data.outgoing = outgoing;
      if (outingDeep) data.outingDeep = outingDeep;
    }
  }
  // 递归
  if (data.childNode) {
    handelGetEveyNode(data.childNode, keyWithGoing);
  }
  if (data.conditionNodes && data.conditionNodes.length) {
    data.conditionNodes.forEach((k) => {
      handelGetEveyNode(k, keyWithGoing);
    });
  }
};

const setGoingForNot = (arrData = [], configData) => {
  // 处理没设置going的剩余节点们
  if (!arrData || !arrData.length) return;
  const findParentDeepGo = (current, prevId, baseData) => {
    // 找到节点上级的outingDeep并赋值给节点
    let pre = getPreviousNode(prevId, baseData);
    if (!pre) return;
    let { outingDeep, prevId: prevIdChild, nodeId } = pre || {};
    if (outingDeep && outingDeep.length) {
      if (!handleIfSet(current, outingDeep)) {
        findParentDeepGo(current, prevIdChild, baseData);
      } else if (!handleSameIdAndPrevId(current, outingDeep)) {
        current.outgoing = outingDeep;
      }
      return;
    }
    findParentDeepGo(current, prevIdChild, baseData);
  };

  const handleIfSet = (data, arrs) => {
    //如果节点的outgoing里包含自身，说明err，继续往上找
    if (!isObject(data) || !arrs || !arrs.length) return false;
    let { nodeId } = data;
    let tempArr = arrs.map((k) => k.nodeId);
    return !tempArr.includes(nodeId);
  };

  const handleSameIdAndPrevId = (data, arrs) => {
    if (!isObject(data) || !arrs || !arrs.length) return false;
    let { prevId } = data;
    let tempArr = arrs.map((k) => k.nodeId);
    return tempArr.includes(prevId);
  };
  arrData.forEach((element) => {
    findParentDeepGo(element, element.prevId || "", configData);
  });
  // console.log(JSON.stringify(arrData));

  // 设置数组对象
  let tempObj = {};
  arrData.forEach((k) => {
    let { nodeId, outgoing } = k || {};
    if (!nodeId || !outgoing) return;
    if (!tempObj[nodeId]) tempObj[nodeId] = {};
    tempObj[nodeId].outgoing = outgoing;
  });
  // console.log(JSON.stringify(tempObj));
  return tempObj || {};
};

const handleNodeNogoging = (baseData) => {
  // 拿到每个节点下还没outging的节点
  let tempArr = [];
  let innerFn = (dataFinal) => {
    if (!isObject(dataFinal)) return;
    let {
      childNode: cnFirst,
      conditionNodes: cdFirst,
      nodeId,
      content,
      prevId,
      outgoing,
      outingDeep,
    } = dataFinal;
    if (!cnFirst && !outgoing)
      tempArr.push({
        nodeId,
        content,
        outingDeep,
        prevId,
      });

    if (cnFirst) {
      let r1 = innerFn(cnFirst);
      if (r1) return r1;
    }

    if (cdFirst) {
      for (let c of cdFirst) {
        let r2 = innerFn(c);
        if (r2) {
          return r2;
        }
      }
    }
  };
  innerFn(baseData);
  // return tempArr;
  return setGoingForNot(tempArr, baseData);
};

const handleOutFormat = (data) => {
  //处理outgoing里的数据格式
  if (!isObject(data)) return;
  let { content, nodeId, type } = data || {};
  return {
    content,
    nodeId,
    type,
  };
};

module.exports = {
  getPreviousNode,
  handelGetEveyNode,
  setGoingForNot,
  handleNodeNogoging,
  handleOutFormat,
  isObject,
};
